package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Major1;
import com.woniu.pojo.*;
import com.woniu.service.DeptService;
import com.woniu.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("major")
public class MajorController {
    @Autowired
    MajorService majorService;
    //院系
    @Autowired
    DeptService deptService;
    /**
     *
     *展示页面跳转
     */
    @RequestMapping("listJump")
    public String listJump(Model model){
        List<Dept> deptList= deptService.selectAll();
        model.addAttribute("deptList",deptList);
        return "WEB-INF/major/list";
    }
    /**
     *  增加页面跳转
     */
    @RequestMapping("addJump")
    public String addJump(Model model){
        List<Dept> deptList= deptService.selectAll();
        model.addAttribute("deptList",deptList);
        return "WEB-INF/major/add";
    }
    /*增加*/
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Major major){
        Map<String,Object> map = new HashMap<>();
        try {
            majorService.add(major);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*删除*/
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer majorId){
        Map<String,Object> map = new HashMap<>();
        try {
            majorService.deleteById(majorId);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*批量删除*/
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Map<String,Object> deleteByIds(Integer[] majorIds){
        Map<String,Object> map = new HashMap<>();
        try {
            majorService.deleteByIds(majorIds);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*查单个*/
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer majorId){
        Map<String,Object> map = new HashMap<>();
        try {
            Major major =majorService.findOne(majorId);
            map.put("major",major);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /**
     *  跳转到修改
     */
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer majorId){
        List<Dept> deptList= deptService.selectAll();
        Major major =majorService.findOne(majorId);
        model.addAttribute("deptList",deptList);
        model.addAttribute("major",major);
        return "WEB-INF/major/update";
    }
    /*改*/
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String,Object> updateById(Major major){
        Map<String,Object> map = new HashMap<>();
        try {
            System.out.println("**********************");
            System.out.println(major);
            System.out.println("**********************");

            majorService.updateById(major);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*查询，按条件*/
    @RequestMapping("selectByExample")
    @ResponseBody
    public Map<String,Object> selectByExample(Major major, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<Major1> majorList = majorService.selectByExample1(major);
            PageHelper.startPage(pageNum,pageSize);
            PageInfo<Major1> pageInfo = new PageInfo<>(majorList);
            map.put("pageInfo",pageInfo);
            map.put("majorList",majorList);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
}
